﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstituteManager.Data;
using InstituteManager.Bussiness;
using System.Data;
using System.Security.Cryptography;

namespace InstituteManager.adm
{
    public partial class SubmissionEvaluation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pnlMessage.Visible = false;
            if (!IsPostBack && Session["Username"] != null)
            {
                gridBind();
            }
        }
        protected void gridBind()
        {
            grvView.DataSource = CompetitionBL.SubmissionEvaluationView(Session["Username"].ToString());
            grvView.DataBind();
        }

        protected void gridPostBind()
        {
            grvPosting.DataSource = CompetitionBL.getListCompetitionPosting(int.Parse(txtCompeId.Text));
            grvPosting.DataBind();
        }

        protected string getMarkString(object Mark)
        {
            if (Mark == null)
                return "";
            switch (int.Parse( Mark.ToString()))
            {
                case 1:
                    return "Best";
                case 2:
                    return "Better";
                case 3:
                    return "Good";
                case 4:
                    return "Moderate";
                case 5:
                    return "Normal";
                case 6:
                    return "Disqualified";
            }
            return "";
        }

        protected string getCompetitionStatusName(string CompetitionId)
        {
            return CompetitionBL.getCompetitionStatusName(int.Parse( CompetitionId));
        }

        protected void GeneralDateControls(DropDownList ddlDay, DropDownList ddlMon, DropDownList ddlYear)
        {
            for (int i = 1; i <= 31; i++)
                ddlDay.Items.Add(new ListItem(i + "", i + ""));
            for (int i = 1; i <= 12; i++)
                ddlMon.Items.Add(new ListItem(i + "", i + ""));
            for (int i = DateTime.Now.Year - 1; i <= DateTime.Now.Year + 20; i++)
                ddlYear.Items.Add(new ListItem(i + "", i + ""));
        }

        protected void ResetControl()
        {
            txtId.Text = "";

        }

        protected void lbtAddnew_Click(object sender, EventArgs e)
        {
            pnlView.Visible = false;
            pnlUpdate.Visible = true;
            ResetControl();
        }

        protected void lbtDelete_Click(object sender, EventArgs e)
        {
            bool result = true;
            bool isChecked = false;
            foreach (GridViewRow row in grvView.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    if (((CheckBox)row.FindControl("chkSelect")).Checked == true)
                    {
                        isChecked = true;
                        if (!CompetitionBL.Delete(int.Parse(grvView.DataKeys[row.RowIndex].Value.ToString())))
                            result = false;
                    }
                }
            }

            if (isChecked == false)
                ShowMessage(-1, "You have not selected data to delete ! ");
            else if (result)
            {
                ShowMessage(1, "Data deleted successfully ! ");
                gridBind();
            }
            else
            {
                ShowMessage(0, "Data deleted fail : " + CompetitionBL.errorMsg);
            }
        }

        protected void lbtRefresh_Click(object sender, EventArgs e)
        {
            gridBind();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            string strId = txtId.Text;
            
            int Mark = 0;
            if (ddlMark.SelectedIndex > 0 )
                Mark = int.Parse(ddlMark.SelectedValue);

            if (PostingBL.TeacherMark(int.Parse(txtCompeId.Text), int.Parse(strId), Mark, txtRemark.Text))
                ShowMessage(1, "You mark posting successfull !");
            else
                ShowMessage(0, "Mark Function fail : " + PostingBL.errorMsg);
            

            pnlPosting.Visible = true;
            pnlUpdate.Visible = false;
            gridPostBind();
        }

        protected void ShowMessage(int MegType, string Message)
        {
            pnlMessage.Visible = true;
            ((Label)pnlMessage.FindControl("lblMessage")).Text = Message;
            switch (MegType)
            {
                case 1:
                    pnlMessage.CssClass = "sukses";
                    break;
                case 0:
                    pnlMessage.CssClass = "gagal";
                    break;
                default:
                    pnlMessage.CssClass = "informasi";
                    break;
            }
        }

        protected void lbtBack_Click(object sender, EventArgs e)
        {
            pnlPosting.Visible = true;
            pnlUpdate.Visible = false;
        }

        protected void grvView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvView.PageIndex = e.NewPageIndex;
            gridBind();
        }

        protected void grvView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strId = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "cmdViewPosts":
                    if (!CompetitionBL.isExpired(int.Parse(strId)))
                    {
                        ShowMessage(-1, "Competition is not finished ! You can't evaluation this Competition !");
                        return;
                    }

                    if (CompetitionBL.isAwardIssued(int.Parse(strId)))
                    {
                        ShowMessage(-1, "Award Issued ! You can't do this function !");
                        return;
                    }

                    grvPosting.DataSource = CompetitionBL.getListCompetitionPosting(int.Parse(strId));
                    grvPosting.DataBind();
                    txtCompeId.Text = strId;

                    pnlView.Visible = false;
                    pnlPosting.Visible = true;
                    break;
                case "cmdMark":
                    tbl_Posting obj = PostingBL.getById(int.Parse(strId));
                    lblPostingName.Text = obj.PostingName;
                    if (obj.PostDate != null)
                        lblPostDate.Text = obj.PostDate.Value.ToString("d");
                    lblDescription.Text = obj.Description;
                    if (obj.tbl_Student != null)
                        lblStudentName.Text = obj.tbl_Student.StudentName;
                    imgImage.ImageUrl = obj.ImageUrl;

                    if (obj.Mark > 0 && obj.Mark <= 6)
                        ddlMark.SelectedValue = obj.Mark.ToString();
                    else
                        ddlMark.SelectedIndex = 0;

                    txtId.Text = strId;
                    pnlUpdate.Visible = true;
                    pnlPosting.Visible = false;
                    break;
                case "cmdReward":
                    if (CompetitionBL.isAwardIssued(int.Parse(strId)))
                    {
                        ShowMessage(-1, "Award Issued ! You can't do this function !");
                        return;
                    }

                    if (!CompetitionBL.isExpired(int.Parse(strId)))
                    {
                        ShowMessage(-1, "Competition is not finished ! You can't do it !");
                        return;
                    }

                    if (!CompetitionBL.isMarkAllPost(int.Parse(strId)))
                    {
                        ShowMessage(-1, "Some Postings haven't got Mark ! You need evaluation all Postings before Award Issued !");
                        return;
                    }

                    if (CompetitionBL.RewardCompetition(int.Parse(strId)))
                        ShowMessage(1, "Competition rewarded successfully ! ");
                    else
                        ShowMessage(-1, "Competition rewarded fail : " + CompetitionBL.errorMsg);

                    gridBind();
                    break;
                
            }
        }

        protected void grvView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
                ((CheckBox)e.Row.FindControl("chkSelectAll")).Attributes.Add("onClick", "SelectAllClick(this)");
            if (e.Row.RowType == DataControlRowType.DataRow)
                ((CheckBox)e.Row.FindControl("chkSelect")).Attributes.Add("onClick", "SelectClick(this)");
        }



        protected void ibtSearch_Click(object sender, ImageClickEventArgs e)
        {
            grvView.DataSource = CompetitionBL.SearchByName(txtSearch.Text);
            DataBind();
        }

        protected void lblBackPost_Click(object sender, EventArgs e)
        {
            pnlPosting.Visible = false;
            pnlView.Visible = true;
        }

        protected void grvPosting_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvPosting.PageIndex = e.NewPageIndex;
            gridPostBind();
        }

        protected void lbtRefreshPost_Click(object sender, EventArgs e)
        {
            gridPostBind();
        }

        protected bool getAwardIssued(string CompetitionId)
        {
            return CompetitionBL.isAwardIssued(int.Parse(CompetitionId));
        }
    }
}